# coding:utf-8
import socket
import re 
from multiprocessing import Process

HTML_ROOT_DIR = "./Views"

class HTTPServer(object):
    def __init__(self):
        """开始方法"""
        self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #设置最多连接数
        print('socket.AF_INET', socket.AF_INET)
        print('socket.SOCK_STREAM', socket.SOCK_STREAM)
    def start(self):
        self.server_socket.listen(128)
        print('服务器等待客户端连接...')
        # 执行死循环
        while True:
            client_socket, client_address = self.server_socket.accept() # 建立客户端连接
            print("[%s，%s]用户连接上了" % client_address)
            # 实例化进程类
            handle_client_process = Process(target=self.handle_client, args=(client_socket,))
            handle_client_process.start()
            client_socket.close()
    def handle_client(self, client_socket):
        """处理客户端请求"""
        # 获取客户端请求数据
        request_data = client_socket.recv(1024)
        print("request data:", request_data)
        request_lines = request_data.splitlines()
        # 输出每行信息
        for line in request_lines:
          print(line)
        request_start_line = request_lines[0]
        print("*" * 10)
        print(request_start_line.decode("utf-8"))
        # 使用正则表达式，提取用户请求的文件名
        file_name = re.match(r"\w+ +(/[^ ]*) ", request_start_line.decode("utf-8")).group(1)
        print('file_name', file_name)
        # 如果文件名是根目录，设置文件名为file_name
        if "/" == file_name:
          file_name = "/index.html"
          print('111', file_name)
        # 打开文件，读取内容
        try:
            print('==', HTML_ROOT_DIR +file_name)
            # file = open(HTML_ROOT_DIR +file_name, "rb")
            file = open('chapter/15/15.1.4/Views/' + file_name, "rb")
        except IOError:
            response_start_line = "HTTP/1.1 404 Not Found\r\n"
            response_headers = "Server: My server\r\n"
            response_body = "The file is not found!"
        else:
            #读取文件内容
            file_data = file.read()
            file.close()
            #构造响应数据
            response_start_line = "HTTP/1.1 200 ok\r\n"
            response_headers = "Server: My server\r\n"
            response_body = file_data.decode("utf-8")
        # 拼接返回数据
        response = response_start_line + response_headers + "\r\n" + response_body
        print("response data:", response)
        client_socket.send(bytes(response, "utf-8"))
        client_socket.close()

    def bind(self, port):
        """绑定端口"""
        self.server_socket.bind(("", port))

def main():
    http_server = HTTPServer()
    http_server.bind(8000)
    http_server.start()

if __name__ == "__main__":
    main()        
          
        